package com.dz.lm.web.controller.newuser.uersend;

import com.alibaba.fastjson.JSONObject;
import com.dz.lm.common.exception.DaoException;
import com.dz.lm.common.exception.ServiceException;
import com.dz.lm.common.util.StringUtil;
import com.dz.lm.common.utils.json.JsonUtils;
import com.dz.lm.domain.userproduct.UserProductExample;
import com.dz.lm.service.pay.WxPayService;
import com.dz.lm.service.userproduct.UserProductService;
import com.dz.lm.service.usersend.UserSendService;
import com.dz.lm.web.controller.BasicControl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;
import java.util.Vector;

/**邀请代理控制层
 * @version 1.8;
 * @author: LDY;
 * @Description;
 * @Date: Created in 2020/3/13 21:57;
 */
@RestController
@RequestMapping(value = "/h5/weijie/rewriteUrl/userSend", produces = "application/json")
public class UserSendController extends BasicControl {
    @Autowired
    private UserSendService userSendService;
    @Resource(name = "readUserProductDao")
    private com.dz.lm.dao.userproduct.read.UserProductDao readUserProductDao;
    @Autowired
    private UserProductService userProductService;
    @Autowired
    private WxPayService wxPayService;
    /**
     * 提出邀请
     * @param request
     * @return
     * @throws ServiceException
     */
    @ResponseBody
    @RequestMapping("/add")
    public Map add(HttpServletRequest request) throws ServiceException {
        String result = "01";
        String message="";
        Object data=null;
        JSONObject subjo = new JSONObject();
        String reqData = request.getAttribute("_reqData") != null ? request.getAttribute("_reqData").toString() : "";
        Map<String, Object> param = JsonUtils.readValue(reqData.toString());
        //参数校验
        Vector<String> inparam = new Vector<String>();

        for (String in : inparam) {
            if (!param.containsKey(in)) {
                subjo = new JSONObject();
                subjo.put("result", "01");
                subjo.put("message", "参数" + in + "异常");
                return subjo;
            }
        }
        //提邀用户
        Integer userId   = param.get("userId") != null ? Integer.parseInt(param.get("userId").toString()) : 0;
        //受邀用户
        Integer sendUid = param.get("sendUid") != null ? Integer.parseInt(param.get("sendUid").toString()) : 0;
        //原生产品
        Integer productId = param.get("productId") != null ? Integer.parseInt(param.get("productId").toString()) : 0;
        //小费
        double fee = param.get("fee") != null ? Double.parseDouble(param.get("fee").toString()) : 0;
        //备注
        String remark = param.get("remark") != null ? param.get("remark").toString() : "";
        //小程序标识
        String wxcode = param.get("wxcode") != null ? param.get("wxcode").toString() : "";
        if (userId == 0 || sendUid == 0 || productId == 0 ) {
            message="参数有误";
            result= "02";
        }else {
            //判断是否受邀人 已代理此产品
            UserProductExample ex = new UserProductExample();
            UserProductExample.Criteria cr = ex.createCriteria();
            cr.andProductTypeEqualTo(2);
            cr.andSuperIdEqualTo(productId);
            cr.andUserIdEqualTo(sendUid);
            cr.andStatusNotEqualTo(3);//不为取消代理
            int count=0;
            try {
                count  = readUserProductDao.querCount(ex);
            } catch (DaoException e) {
                e.printStackTrace();
            }
            if(count ==0){
                int sendId = userSendService.add(userId, sendUid,remark,productId,fee,param.get("platform").toString());
                if (sendId > 0) {
                    message="操作成功";
                    result= "00";
                    if(fee>0){
                        Map map = new HashMap();
                        map.put("userId", userId);
                        map.put("amount",fee);
                        map.put("orderType",4);
                        map.put("sendId",sendId);
                        map.put("type",3);//代理邀请
                        map.put("body","代理费");
                        map.put("platform", param.get("platform"));
                        if(!StringUtil.containEmpty(wxcode)){
                            map.put("wxcode", wxcode);
                        }
                        Map<String, Object> retData = wxPayService.unifiedOrder(map);
                        data = retData;
                    }
                }else if (sendId == -998){
                    message="已邀请过该用户代理此产品";
                    result= "01";
                }
            }else if(sendUid == -997){
                result ="02";
                message ="该用户已代理该产品";
            } else {
                result ="03";
                message ="邀请失败";
            }

        }
        if(param.get("platform").equals("Android")){
            Map<String, Object> retData = new HashMap();
            retData.put("message", message);
            retData.put("result",result);
            retData.put("data",data);
            subjo.putAll(retData);
            return parameterEncrypt(request, subjo);
        }else {
            subjo.put("message", message);
            subjo.put("result", result);
            subjo.put("data",data);
            return  subjo;
        }
    }
    /**
     * 处理邀请
     * @param request
     * @return
     * @throws ServiceException
     */
    @ResponseBody
    @RequestMapping("/handle")
    public Map handle(HttpServletRequest request) throws ServiceException {
        String result = "01";
        String message="";
        Object data=null;
        JSONObject subjo = new JSONObject();
        String reqData = request.getAttribute("_reqData") != null ? request.getAttribute("_reqData").toString() : "";
        Map<String, Object> param = JsonUtils.readValue(reqData.toString());
        //参数校验
        Vector<String> inparam = new Vector<String>();

        for (String in : inparam) {
            if (!param.containsKey(in)) {
                subjo = new JSONObject();
                subjo.put("result", "01");
                subjo.put("message", "参数" + in + "异常");
                return subjo;
            }
        }
        //id
        Integer sendId = param.get("sendId") != null ? Integer.parseInt(param.get("sendId").toString()) : 0;
        //0-发送1接受-2-拒绝
        Integer status = param.get("status") != null ? Integer.parseInt(param.get("status").toString()) : 0;

        if (sendId == 0 ) {
            message="参数有误";
            result= "02";
        }else {
            int i = userSendService.handle(sendId,status,param.get("platform").toString());
            if (i > 0) {
                message="操作成功";
                result= "00";
            }
        }
        if(param.get("platform").equals("Android")){
            Map<String, Object> retData = new HashMap();
            retData.put("message", message);
            retData.put("result",result);
            retData.put("data",data);
            subjo.putAll(retData);
            return parameterEncrypt(request, subjo);
        }else {
            subjo.put("message", message);
            subjo.put("result", result);
            subjo.put("data",data);
            return  subjo;
        }
    }
    /**
     * 获取受邀列表
     *
     * @param request
     * @return
     * @throws ServiceException
     */
    @ResponseBody
    @RequestMapping("/sendsList")
    public Map sendsList(HttpServletRequest request) throws ServiceException {
        String result = "01";
        String message = "";
        Object data = null;
        JSONObject subjo = new JSONObject();
        String reqData = request.getAttribute("_reqData") != null ? request.getAttribute("_reqData").toString() : "";
        String uId = request.getAttribute("userId")!=null?request.getAttribute("userId").toString():"";
        Map<String, Object> param = JsonUtils.readValue(reqData);

        //参数校验
        Vector<String> inparam = new Vector<String>();
        //受邀请人
        Integer userId = param.get("userId") != null ? Integer.parseInt(param.get("userId").toString()) : 0;
        //0-发送1接受-2-拒绝
        Integer status = param.get("status") != null ? Integer.parseInt(param.get("status").toString()) : 0;
        Integer pageNum = param.get("page") != null ? Integer.parseInt(param.get("page").toString()) : 1;
        Integer rows = param.get("rows") != null ? Integer.parseInt(param.get("rows").toString()) : 10;
        data = userSendService.query(pageNum+1, rows, 0,userId,0,status);
        message = "获取成功";
        result = "00";
        if (param.get("platform").equals("Android")) {
            Map<String, Object> retData = new HashMap();
            retData.put("message", message);
            retData.put("result", result);
            retData.put("data", data);
            subjo.putAll(retData);
            return parameterEncrypt(request, subjo);
        } else {
            subjo.put("message", message);
            subjo.put("result", result);
            subjo.put("data", data);
            return subjo;
        }
    }
}
